using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.StsAdmin;
using Microsoft.SharePoint;

namespace CKS.WindowsLiveAuthentication
{
    class EventReceiverStsAdmCommand : ISPStsadmCommand
    {


        #region ISPStsadmCommand Members

        public string GetHelpMessage(string command)
        {
            StringBuilder ret = new StringBuilder();
            ret.AppendFormat("stsadm -o managereceivers\n");
            ret.AppendFormat("Required flags:\n");
            ret.AppendFormat("\t-url <URL of the object you want to manage receivers on>\n");
            ret.AppendFormat("\t-type <Type of object: Web/List/Item>\n");
            ret.AppendFormat("\t-action <add/remove/list>\n");
            ret.AppendFormat("\t-data <Data to act on>\n");
            ret.AppendFormat("\t-list <list name to act on>\n");

            return ret.ToString();
        }

        public int Run(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)
        {
            if (!keyValues.ContainsKey("url") || !keyValues.ContainsKey("action") || !keyValues.ContainsKey("data"))
            {
                output = GetHelpMessage("");
                return 1;
            }
            string url = keyValues["url"];
            string action = keyValues["action"];
            string data = keyValues["data"];
            string listName = keyValues["list"];

            using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[listName];
                    Console.WriteLine("List: " + list.Title);

                    string[] addData = data.Split(';');

                    SPEventReceiverType rtype = SPEventReceiverType.ItemAdding;
                    string rclass = string.Empty;
                    string rassembly = string.Empty;
                    if (addData.Length != 3)
                    {
                        rtype = (SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), addData[0]);
                        rclass = addData[1];
                        rassembly = addData[2];
                    } 

                    if (action == "add")
                    {
                        if (rclass == string.Empty || rassembly == string.Empty)
                        {
                            list.EventReceivers.Add(rtype, rassembly, rclass);
                        }
                    }
                    else if (action == "list")
                    {
                        foreach (SPEventReceiverDefinition def in list.EventReceivers)
                        {
                            Console.WriteLine("{0}, {1}, {2}", def.Type, def.Class, def.Assembly);
                        }
                    }
                    else
                    {
                        foreach (SPEventReceiverDefinition def in list.EventReceivers)
                        {
                            if (def.Type == rtype && def.Class == rclass && def.Assembly == rassembly)
                            {
                                def.Delete();
                            }
                        }
                    }

                    list.Update();
                }
            }

            output = "";
            return 0;
        }

        #endregion
    }
}
